﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYBYSoft.Company.Models;

namespace HYBYSoft.Company.WebSite.Areas.Admin.Controllers
{
    public class EventLogController : Controller
    {
        /// <summary>
        /// 数据库实体上下文对象
        /// </summary>
        CompanySiteEntities _dbContext = new CompanySiteEntities();

        /// <summary>
        /// 事件日志Get请求
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult Index(string txtTitle, DateTime? startTime, DateTime? endTime)
        {
            //获得查询条件
            string name = txtTitle ?? string.Empty;
            DateTime start = startTime.HasValue ? startTime.Value : DateTime.MinValue;
            DateTime end = endTime.HasValue ? endTime.Value : DateTime.MaxValue;

            //查询符合条件的日志信息
            var eventLog = from l in _dbContext.EventLogs
                           where l.EventMessage.Contains(name) && (l.EventTime >= start && l.EventTime <= end)
                           orderby l.EventTime descending
                           select l;

            return View(eventLog);
        }

        /// <summary>
        /// 日志查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            return RedirectToAction("Index", new { txtTitle = Request["txtTitle"], startTime = Request["startTime"], endTime = Request["endTime"] });
        }

        /// <summary>
        /// 日志详细查看页面Get请求
        /// </summary>
        /// <param name="logId">事件日志编号</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult Details(int logId, string pageId)
        {
            ViewBag.PageId = pageId;

            var log = _dbContext.EventLogs.FirstOrDefault(l => l.EventID == logId);

            return View(log);
        }
    }
}
